const { resolve } = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");

const entry = {
  main: resolve(__dirname, 'src', 'index.js'),
  second: resolve(__dirname, 'src', 'second.js'),
}

const output = {
  path: resolve(__dirname, 'build'),
  filename: '[name].js',
  clean: true,
  assetModuleFilename: 'assets/[name][ext]'
}

const plugins = [
  new HtmlWebpackPlugin({
    template: resolve(__dirname, 'public', 'index.html')
  }),
  new MiniCssExtractPlugin({
    filename: 'style/[name].css'
  }),
]

const MODULE = {
  rules: [
    {
      test: /\.css$/, // 匹配以.css结尾的文件
      use: [MiniCssExtractPlugin.loader, 'css-loader'], // 使用 css-loader 处理
    },
    {
      test: /\.(png|jpeg|jpg)$/i,
      type: 'asset/resource',
      generator: {
        filename: 'assets/image/[name][ext]'
      },
    },
    {
      test: /\.(svg)$/i,
      type: 'asset/resource',
      generator: {
        filename: 'assets/svg/[name][ext]'
      },
    }
  ]
}

module.exports = {
  entry, 
  output,
  plugins,
  module: MODULE,
  mode: 'development',
}